using UnityEngine;
using UnityEngine.Playables;
using TreeDesigner;

namespace Taco.Timeline
{
    #region Value
    public abstract class TimelineActionNode : ActionNode
    {
        public TimelineRunningTree TimelineRunningTree => Owner as TimelineRunningTree;
        public TreeClipPlayable ClipPlayable => TimelineRunningTree.ClipPlayable;
        public Timeline Timeline => ClipPlayable.Timeline;
    }

    [NodeName("MuteTrack")]
    [NodePath("Timeline/Action/MuteTrack")]
    public class MuteTrackNode : TimelineActionNode
    {
        [SerializeField, PropertyPort(PortDirection.Input, "TrackIndex")]
        IntListPropertyPort m_TrackIndex = new IntListPropertyPort();
        [SerializeField, PropertyPort(PortDirection.Input, "Mute")]
        BoolPropertyPort m_Mute = new BoolPropertyPort();

        protected override void DoAction()
        {
            foreach (int index in m_TrackIndex.Value)
            {
                Timeline.RuntimeMute(index, m_Mute.Value);
            }
        }
    }

    [NodeName("TimeJump")]
    [NodePath("Timeline/Action/TimeJump")]
    public class TimeJumpNode : TimelineActionNode
    {
        [SerializeField, PropertyPort(PortDirection.Input, "TargetTime")]
        FloatPropertyPort m_TargetTime = new FloatPropertyPort();

        protected override void DoAction()
        {
            Timeline.JumpTo(m_TargetTime.Value);
        }
    }
    #endregion

    #region Value
    public abstract class TimelineValueNode :ValueNode
    {
        public TimelineRunningTree TimelineRunningTree => Owner as TimelineRunningTree;
        public TreeClipPlayable ClipPlayable => TimelineRunningTree.ClipPlayable;
        public Timeline Timeline => ClipPlayable.Timeline;
    }

    [NodeName("TimelineTime")]
    [NodePath("Timeline/Value/TimelineTime")]
    public class TimelineTimeNode : TimelineValueNode
    {
        [SerializeField, PropertyPort(PortDirection.Output, "TimelineTime"), ReadOnly]
        FloatPropertyPort m_TimelineTime = new FloatPropertyPort();
        [SerializeField, PropertyPort(PortDirection.Output, "ClipTime"), ReadOnly]
        FloatPropertyPort m_ClipTime = new FloatPropertyPort();

        protected override void OutputValue()
        {
            base.OutputValue();
            m_TimelineTime.Value = Timeline.Time;
            m_ClipTime.Value = (float)ClipPlayable.Handle.GetTime();
        }
    }
    #endregion
}
